import Vue from 'vue'
import Vuex from 'vuex'
import {
  reqSaveOrUpdateList,
  reqUserRegister,
  reqUserLogin,
  reqLoginOut
} from '@/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    titleList: {}
  },
  mutations: {
    SAVEORUPDATELIST(state, titleList) {
      state.titleList = titleList
    }
  },
  actions: {
    // 用户注册
    async addUserRegisteer({commit }, data) {
      let result = await reqUserRegister(data)
      // console.log(result);
      if (result.code === 200) {
        return "OK"
      } else {
        //返回的是失败的标记
        return Promise.reject(new Error("fail"));
      }
    },

    // 用户登录
    async userLogin({commit }, data){
      let result = await reqUserLogin(data)
      console.log(result);
      if(result.success == true){
        return "OK"
      } else {
        //返回的是失败的标记
        return Promise.reject(new Error("fail"));
      }
    },
    //  退出登录
    async loginOut(){
      let result = await reqLoginOut()
      console.log(result);
      if(result.success == true){
        return "OK"
      } else {
        //返回的是失败的标记
        return Promise.reject(new Error("fail"));
      }
    },
    
    // 新增文章
    async saveOrUpdateList({commit}, data) {
      let result = await reqSaveOrUpdateList(data)
      // console.log(data);
      // console.log(result);
      if (result.code == 200) {
        return "OK"
      } else {
        //返回的是失败的标记
        return Promise.reject(new Error("fail"));
      }
    }
  },
  modules: {}
})